#include "MsgQueue.h"
#include "pthread.h"

MsgQueue::MsgQueue():mHandle(NULL_HANDLE)
{
	char s_name[16+1];
	status_t ret = RET_OK;

	ret = msg_q_create(&mHandle, s_name, sizeof(Msg_T), 512);
}

MsgQueue::~MsgQueue()
{
	status_t ret = RET_OK;

	if (mHandle != NULL_HANDLE)
	{
		ret = msg_q_delete(mHandle);
		mHandle = NULL_HANDLE;
	}
}

status_t MsgQueue::SendMessage(Msg_T *msg)
{
	int ret;
	int count = 0;

	if(NULL_HANDLE == mHandle)
	{
		return RET_FAIL;
	}

	while((ret = msg_q_send(mHandle, (const VOID *)msg, sizeof(Msg_T), 1)) != RET_OK)
	{
		count++;
		if(count > 2)
			break;
	}

	return ret;
}

void MsgQueue::ReceiveMessage(Msg_T *msg)
{
	size_t msg_size = 0;
	UINT16	index = 0;

	msg_size = sizeof(Msg_T);

	if(NULL_HANDLE == mHandle)
	{
		return;
	}

	while(msg_q_receive(&index, msg, &msg_size, &mHandle, 1, MSG_Q_OPTION_WAIT) != RET_OK)
	{
		continue;
	}
}